window._MTZ = {

    _MAINLABEL:null,
    _PAGENMB:0,
    _PAGESIZE:8,
    _CHAPTERINDEX:0,

    _TZARR:[],

    _PLAYERARR:[],

    _ARMYARR:[],

    _RULE:[
        "1.使用系统队伍进行战斗,挑战成功后获得对应修为的符文(可用于装备镶嵌)。"
        ,"2.每解锁2%全图鉴会开放一个新的挑战关卡"
    ],

    isChapterOpen:function(i){

        var perObj = _MANAGERTJ.getTotalPer();
        var per = perObj.nmb/perObj.total;
        if(i==0){
            return true;
        }
        return i<=(per/0.02);

    },

    getTipsMsg:function(){
        var tipsmsg = _MTZ._RULE.join("\n");
        var perObj = _MANAGERTJ.getTotalPer();
        var per = perObj.nmb/perObj.total;
        var nmb = 3;
        var realper =  (per*100).toFixed(nmb)+"%";
        tipsmsg+="\n3.当前全图鉴进度:"+realper;

        return tipsmsg;
    },

    isTzModel:function(){

        return window.fightData.tzChapter==0||window.fightData.tzChapter;

    },

    closeUi:function(){
        if(_MTZ._MAINLABEL){
            _GRM.putNode(_MTZ._MAINLABEL);
            _MTZ._MAINLABEL = null;
        }
    },

    showUi:function(parentNode,param){

        var uiObj = _GRM.getNodeObj('tzui');
        var mainLabel = uiObj.node;
        _MTZ._MAINLABEL = mainLabel;
       
        _MTZ._PAGENMB = 0;

        if(uiObj.isnew){
            _MTZ.initButton(mainLabel);
        }

        parentNode.addChild(mainLabel);

        _MTZ.refreshAll();

        return mainLabel;

    },

    initButton:function(mainLabel){
        
        var maskNode = mainLabel.getChildByName('mask');
        maskNode.on(cc.Node.EventType.TOUCH_START, function(){
            _MTZ.closeUi();
        }, maskNode);

        var infobtn = mainLabel.getChildByName('buttonInfo');
        infobtn.on(cc.Node.EventType.TOUCH_START, function(){
            _TIPS.showMessage(_MTZ.getTipsMsg());
        }, infobtn);


        var buttonCj = mainLabel.getChildByName('buttonCj');
        buttonCj.on(cc.Node.EventType.TOUCH_START, function(){
            _MTZCJ.showUi(_MAP.node,null);
        }, buttonCj);


        var leftPageNode = mainLabel.getChildByName('leftLabel')
                            .getChildByName('page');
        var leftPageNext = leftPageNode.getChildByName('btnRight');
        var leftPageLast = leftPageNode.getChildByName('btnLeft');
        leftPageNext.on(cc.Node.EventType.TOUCH_START, function(){
            //console.log('关卡下一页');
            _MTZ._PAGENMB++;
            _MTZ.refreshAll();
        }, leftPageNext);
        leftPageLast.on(cc.Node.EventType.TOUCH_START, function(){
            //console.log('关卡上一页');
            if(_MTZ._PAGENMB>0)
            _MTZ._PAGENMB--;
            _MTZ.refreshAll();
        }, leftPageLast);


        var rightPageNode = mainLabel.getChildByName('rightLabel')
                            .getChildByName('background')
                            .getChildByName('page');
        var rightPageNext = rightPageNode.getChildByName('btnRight');
        var rightPageLast = rightPageNode.getChildByName('btnLeft');
        rightPageNext.on(cc.Node.EventType.TOUCH_START, function(){
            //console.log('人员下一页');
        }, rightPageNext);
        rightPageLast.on(cc.Node.EventType.TOUCH_START, function(){
            //console.log('人员上一页');
        }, rightPageLast);


        var jubenNodes = mainLabel.getChildByName('leftLabel')
                                .getChildByName('troops')
                                .getChildByName('troops')
                                .getChildren();
        for(var i=0;i<jubenNodes.length;i++){
            var jubenNode = jubenNodes[i];
            var parentNode = jubenNode.getChildByName('information');
            var btnTz = parentNode.getChildByName('buttonInfo copy');
            var btnTzAgain = parentNode.getChildByName('buttonInfoAgain');
            var btnDetail = parentNode.getChildByName('buttonInfo');
            //var label = parentNode.getChildByName('buttonInfo copy');
            btnTz.on(cc.Node.EventType.TOUCH_START, function(){
                console.log('挑战:'+this.tzIndex);
                /*
                if(_MTZ.canTz(this.tzIndex,window.gameData.playerTeam)){
                    _MAP.jumpToTz(this.tzIndex,window.gameData.playerTeam);
                }else{
                    _TIPS.showMessage('出阵人员不符');
                }*/
                _MAP.jumpToTz(this.tzIndex,window.gameData.playerTeam);
            }, jubenNode);
            btnDetail.on(cc.Node.EventType.TOUCH_START, function(){
                //console.log('奖励:'+this.tzIndex);
                _MTZ.showRight(this.tzIndex);
            }, jubenNode);
        }

        var playerNodes = mainLabel.getChildByName('rightLabel')
                            .getChildByName('background')
                            .getChildByName('goods').getChildren();
        
        var armyNodes = mainLabel.getChildByName('rightLabel')
                            .getChildByName('background')
                            .getChildByName('goods2').getChildren();

        for(var i=0;i<playerNodes.length;i++){
            playerNodes[i].roleIndex=i;
            playerNodes[i].on(cc.Node.EventType.TOUCH_START, function(){
                if(_MTZ._PLAYERARR[this.roleIndex]){
                    _NPCDL.initUi(_MAP.node,{sence:_STYPE._FIGHTSENCE,
                        roleArr:_MTZ._PLAYERARR,
                        roleIndex:this.roleIndex
                    });
                }
            }, playerNodes[i]);
        }

        for(var i=0;i<armyNodes.length;i++){
            armyNodes[i].roleIndex=i;
            armyNodes[i].on(cc.Node.EventType.TOUCH_START, function(){
                if(_MTZ._ARMYARR[this.roleIndex]){
                    _NPCDL.initUi(_MAP.node,{sence:_STYPE._FIGHTSENCE,
                        roleArr:_MTZ._ARMYARR,
                        roleIndex:this.roleIndex
                    });
                }
            }, armyNodes[i]);
        }
    },

    canTz:function(tzIndex,playerTeam){

        if(playerTeam==null){
            return false;
        }

        var conf = _CTZ[tzIndex];
        if(playerTeam.length==conf.player.length){
            for(var i=0;i<conf.player.length;i++){
                var has = false;
                for(var j=0;j<playerTeam.length;j++){
                    if(playerTeam[j].npctype==conf.player[i]){
                        has=true;
                    }
                }
                if(!has){
                    return false;
                }
            }
        }else{
            return false;
        }

        return true;
    },

    getTzArr:function(){

        _MTZ._TZARR.length=0;

        var beginIndex = _MTZ._PAGESIZE*_MTZ._PAGENMB;

        for(var i=0;i<_MTZ._PAGESIZE;i++){

            var realIndex = beginIndex+i;
            if(_CTZ[realIndex]){
                _MTZ._TZARR.push({obj:_CTZ[realIndex],index:realIndex});
            }

        }

    },

    refreshAll:function(){

        _MTZ.getTzArr();

        _MTZ.hideRight();

        var mainLabel = _MTZ._MAINLABEL;

        var jubenNodes = mainLabel.getChildByName('leftLabel')
                                .getChildByName('troops')
                                .getChildByName('troops')
                                .getChildren();

       mainLabel.getChildByName('leftLabel').getChildByName('page')
                .getChildByName('label').getComponent(cc.Label).string = "第"+(_MTZ._PAGENMB+1)+"页";

        for(var i=0;i<jubenNodes.length;i++){

            var jubenNode = jubenNodes[i];
            var parentNode = jubenNode.getChildByName('information');
            var label = parentNode.getChildByName('label');
            if(_MTZ._TZARR[i]&&_MTZ.isChapterOpen(_MTZ._TZARR[i].index)){
                
                jubenNode.active = true;
                var btnTzAgain = parentNode.getChildByName('buttonInfoAgain');
                if(_GAMEDATAHELPER.getTzLog(_MTZ._TZARR[i].index)){
                    btnTzAgain.active = true;
                }else{
                    btnTzAgain.active = false;
                }

                label.getComponent(cc.Label).string = _MTZ._TZARR[i].obj.name;
                jubenNode.tzIndex = _MTZ._TZARR[i].index;
            }else{
                jubenNode.active = false;
            }
        }
    },

    hideRight:function(){

        var mainLabel = _MTZ._MAINLABEL;

        var playerNodes = mainLabel.getChildByName('rightLabel')
                            .getChildByName('background')
                            .getChildByName('goods').getChildren();
        
        var armyNodes = mainLabel.getChildByName('rightLabel')
                            .getChildByName('background')
                            .getChildByName('goods2').getChildren();

        for(var i=0;i<playerNodes.length;i++){
            playerNodes[i].active = false;
        }

        for(var i=0;i<armyNodes.length;i++){
            armyNodes[i].active = false;
        }
    },

    showRight:function(tzIndex){

        if(_CTZ[tzIndex]){

            var playerArr = _CTZ[tzIndex].player;

            var armyArr = _CTZ[tzIndex].army;

            var mainLabel = _MTZ._MAINLABEL;

            var playerNodes = mainLabel.getChildByName('rightLabel')
                                .getChildByName('background')
                                .getChildByName('goods').getChildren();
            
            var armyNodes = mainLabel.getChildByName('rightLabel')
                                .getChildByName('background')
                                .getChildByName('goods2').getChildren();

            _MTZ._PLAYERARR.length=0;
            for(var i=0;i<playerArr.length;i++){
                _MTZ._PLAYERARR.push({value:playerArr[i]});
            }

            _MTZ._ARMYARR.length=0;
            for(var i=0;i<armyArr.length;i++){
                _MTZ._ARMYARR.push({value:armyArr[i]});
            }

            for(var i=0;i<playerNodes.length;i++){
                if(playerArr[i]){
                    _MTZ.refreshOne(playerNodes[i],playerArr[i]);
                }else{
                    playerNodes[i].active = false;
                }
            }

            for(var i=0;i<armyNodes.length;i++){
                if(armyArr[i]){
                    _MTZ.refreshOne(armyNodes[i],armyArr[i]);
                }else{
                    armyNodes[i].active = false;
                }
            }

        }else{
            _MTZ.hideRight();
        }
    },

    refreshOne:function(node,npctype){
        var npctypeConfig = _NPC[npctype];
        if(npctypeConfig){

            var npcjob = npctypeConfig.job;
            var npcrace = npctypeConfig.race;
            var npcqua = npctypeConfig.level;

            node.active = true;
            node.roleName = npctype;

            var faceNode = node.getChildByName('touxian');
            var nameNode = node.getChildByName('name');
            var ftypeNode = node.getChildByName('ftype');
            var ftypeStrNode = ftypeNode.getChildByName('ftypeStr');

            var jobNode = node.getChildByName('job');
            var jobStrNode = jobNode.getChildByName('ftypeStr');
            jobNode.color = new cc.color(window._JOB[npcjob].color);
            jobStrNode.getComponent(cc.Label).string = window._JOB[npcjob].sname;
            var zhongNode = node.getChildByName('zhong');
            var zhongStrNode = zhongNode.getChildByName('ftypeStr');
            zhongNode.color = new cc.color(window._RACE[npcrace].color);
            zhongStrNode.getComponent(cc.Label).string = window._RACE[npcrace].name;
            var quaNode = node.getChildByName('qua');
            var quaStrNode = quaNode.getChildByName('ftypeStr');
            quaNode.color = new cc.color(window._CS.qua[npcqua].color);
            quaStrNode.getComponent(cc.Label).string = npcqua;

            faceNode.getComponent(cc.Sprite).spriteFrame = _GRM.getFaceFrame(npctypeConfig.face);
            nameNode.getComponent(cc.Label).string = npctype;
            ftypeNode.color = new cc.color(_CS.ftype[npctypeConfig.ftype].color);
            ftypeStrNode.getComponent(cc.Label).string = npctypeConfig.ftype;


        }else{

            node.active = false;

        }
        
    },
}